/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.UserManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.UserCreateForm;
import info.peigen.hotpot.manage.core.manage.form.UserEditForm;
import info.peigen.hotpot.manage.core.manage.form.UserSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.UserManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.UserEntity;
import info.peigen.hotpot.manage.core.service.UserService;
import info.peigen.hotpot.manage.core.service.order.UserCreateOrder;
import info.peigen.hotpot.manage.core.service.order.UserEditOrder;
import info.peigen.hotpot.manage.core.service.result.UserCreateResult;
import info.peigen.hotpot.manage.core.service.result.UserEditResult;
import info.peigen.hotpot.manage.core.service.result.info.UserInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(UserManageService)</b>
 * 管理后台 "用户" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class UserManageService {
    @Inject
    UserManageRepository userManageRepository;
    @Inject
    UserService          userService;

    public PageResult<UserInfo> page(UserSearchForm form) {
        LambdaQuery<UserEntity> lambdaQuery = query(form);
        return userManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, UserManageConvertor.INSTANCE::convertToInfo);
    }

    public List<UserInfo> list(UserSearchForm form) {
        LambdaQuery<UserEntity> lambdaQuery = query(form);
        List<UserEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(UserManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public UserInfo create(UserCreateForm form) {
        UserCreateOrder  order  = UserManageConvertor.INSTANCE.convertToCreateOrder(form);
        UserCreateResult result = userService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public UserInfo edit(UserEditForm form) {
        UserEditOrder  order  = UserManageConvertor.INSTANCE.convertToEditOrder(form);
        UserEditResult result = userService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        userManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        userManageRepository.removeByIds(ids);
    }

    public UserInfo detail(Integer id) {
        UserEntity entity = userManageRepository.getMapper().single(id);
        return UserManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public UserInfo detail(String userId) {
        UserEntity entity = userManageRepository.getMapper().findByUserId(userId);
        return UserManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<UserEntity> query(UserSearchForm form) {
        return userManageRepository.getMapper().createLambdaQuery()
                .andLike(UserEntity::getUserId, Query.filterLikeEmpty(form.getUserId(), true, true))
                .andLike(UserEntity::getLoginName, Query.filterLikeEmpty(form.getLoginName(), true, true))
                .andEq(UserEntity::getStatus, Query.filterEmpty(form.getStatus()))
                .andLike(UserEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}